package jdbc.mysql.gzpowernode;

import JDBC_TB.*;

import java.sql.*;

/*
 * mysql事务
 *
 * */
public class test06 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.注册驱动
            //2.获取连接对象
            conn = Jdbc_Tb.getConnection("gzpowernode", false);
            //3.获取数据库预处理操作对象
            String sql = "select ename,sal from emp where sal = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 5000);
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("ename") + "," + rs.getDouble("sal"));
            }
            conn.commit();
        } catch (Exception e) {
            if (conn != null) {
                try {//事务出错将回滚
                    conn.rollback();
                } catch (SQLException a) {
                    a.printStackTrace();
                }
            }
            e.printStackTrace();
        } finally {
            Jdbc_Tb.closeSource(conn, ps, rs);
        }
    }
}

